-
Notifications
You must be signed in to change notification settings - Fork 5
[app_bricks] Add WaveGenerator brick #15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Recent Updates to WaveGenerator BrickThis update refines the WaveGenerator brick's API and improves hardware volume control, along with comprehensive testing and documentation updates. Changes Summary1. External Speaker Instance Support (
|
| def play_sequence(): | ||
| """Play a simple frequency sequence.""" | ||
| frequencies = [261.63, 293.66, 329.63, 349.23, 392.00, 440.00, 493.88, 523.25] # C4 to C5 | ||
| note_names = ["C4", "D4", "E4", "F4", "G4", "A4", "B4", "C5"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would remove refences to notes to do not create misleading with sound generator
| mx = alsaaudio.Mixer(mixer[0]) | ||
| logger.debug(f"Loaded mixer: {mixer[0]} for card {card_name}") | ||
| return mx | ||
| mx = alsaaudio.Mixer("Headset", cardindex=card_index) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not always correct. It is not true that there is always an "Headset".
That is why there was the code that, given a cardindex, try to find the associated mixer.
So, the problem that you faced, should have a different solution rather than this.
Wave Generator Brick
A continuous wave generator for real-time audio synthesis. Generates various waveforms (sine, square, sawtooth, triangle) and streams them to a USB speaker with smooth frequency and amplitude transitions.
Features
Usage
Basic Example
Advanced Configuration
Theremin-Style Controller
With WebUI Control
API Reference
Constructor
Parameters:
sample_rate: Audio sample rate in Hz (default: 16000)wave_type: Initial waveform - "sine", "square", "sawtooth", "triangle" (default: "sine")block_duration: Audio block duration in seconds (default: 0.03)attack: Amplitude attack time in seconds (default: 0.01)release: Amplitude release time in seconds (default: 0.03)glide: Frequency glide time (portamento) in seconds (default: 0.02)speaker_device: Speaker device identifier (default: USB_SPEAKER_1)speaker_format: Audio format (default: "FLOAT_LE")Methods
set_frequency(frequency: float)Set target output frequency with smooth glide transition.
Parameters:
frequency: Target frequency in Hz (typically 20-8000 Hz)set_amplitude(amplitude: float)Set target output amplitude with smooth attack/release.
Parameters:
amplitude: Target amplitude in range [0.0, 1.0]set_wave_type(wave_type: WaveType)Change the waveform type.
Parameters:
wave_type: One of "sine", "square", "sawtooth", "triangle"set_volume(volume: float)Set master volume level.
Parameters:
volume: Master volume in range [0.0, 1.0]set_envelope_params(attack=None, release=None, glide=None)Update envelope parameters.
Parameters:
attack: Attack time in seconds (optional)release: Release time in seconds (optional)glide: Frequency glide time in seconds (optional)get_state() -> dictGet current generator state.
Returns:
frequency,amplitude,wave_type,master_volume,phaseWaveform Types
Sine Wave
Classic smooth sine wave, ideal for pure tones and musical applications.
Square Wave
Sharp square wave with odd harmonics, creates a "hollow" or "clarinet-like" sound.
Sawtooth Wave
Bright sawtooth wave with all harmonics, creates a "buzzy" or "brassy" sound.
Triangle Wave
Softer than square, contains only odd harmonics with lower amplitude.
Envelope Parameters
Attack Time
Time to rise from current amplitude to target amplitude when increasing.
Typical values:
0.001- 1ms: Very fast, almost instant0.01- 10ms: Fast, percussive0.1- 100ms: Slow, pad-likeRelease Time
Time to fall from current amplitude to target amplitude when decreasing.
Typical values:
0.01- 10ms: Short decay0.05- 50ms: Medium decay0.5- 500ms: Long decay, reverb-likeGlide Time (Portamento)
Time to smoothly transition from current frequency to target frequency.
Typical values:
0.0- Disabled: Instant frequency changes (may cause clicks)0.005- 5ms: Minimal, just removes clicks0.02- 20ms: Natural, smooth transitions (recommended)0.05- 50ms: Noticeable portamento effect0.1+- 100ms+: Very "slidey", theremin-likeHardware Requirements
Note: Must run in Network Mode or SBC Mode as the USB-C port is needed for the hub.
Troubleshooting
No Sound Output
wave_gen.set_amplitude(0.5)wave_gen.set_volume(0.8)Choppy or Clicking Audio
wave_gen.set_envelope_params(glide=0.05)WaveGenerator(block_duration=0.02)"No USB speaker found" Error